package com.liang.codegenerate.controller;

import com.liang.codegenerate.common.BaseResponse;
import com.liang.codegenerate.common.ErrorCode;
import com.liang.codegenerate.common.ResultUtils;
import com.liang.codegenerate.exception.BusinessException;
import com.liang.codegenerate.model.dto.tables.TableTestJoinDto;
import com.liang.codegenerate.model.dto.tables.TablesAddDto;

import com.liang.codegenerate.model.dto.tables.TablesListDto;
import com.liang.codegenerate.model.dto.tables.TablesUpdateDto;
import com.liang.codegenerate.model.entity.Tables;
import com.liang.codegenerate.model.vo.tables.TableJoinTestVo;
import com.liang.codegenerate.model.vo.tables.TablesFieldsVo;
import com.liang.codegenerate.service.TablesService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/tables")
public class TablesController {

    @Resource
    private TablesService tablesService;

    @PostMapping("/add")
    public BaseResponse<Boolean> addTable(@RequestBody TablesAddDto tablesAddDto, HttpServletRequest request) {
        if (tablesAddDto == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = tablesService.addTable(tablesAddDto,request);
        return ResultUtils.success(b);
    }

    @DeleteMapping("/delete")
    public BaseResponse<Boolean> deleteTable(Long id) {
        if (id == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = tablesService.deleteTable(id) == 0;
        return ResultUtils.success(b);
    }

    @PutMapping("/update")
    public BaseResponse<Boolean> updateTable(@RequestBody TablesUpdateDto tablesUpdateDto) {
        if (tablesUpdateDto == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = tablesService.updateTable(tablesUpdateDto);
        return ResultUtils.success(b);
    }

    @PostMapping("/list")
    public BaseResponse<List<Tables>> listTable(@RequestBody TablesListDto tablesListDto,HttpServletRequest request) {
        if (tablesListDto == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        List<Tables> list = tablesService.listTable(tablesListDto,request);
        return ResultUtils.success(list);
    }

    @GetMapping("/list/id")
    public BaseResponse<Tables> listTableById(@RequestParam Long id){
        if (id == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Tables tables = tablesService.listTableById(id);
        return ResultUtils.success(tables);
    }

    @GetMapping("/get/fields")
    public BaseResponse<TablesFieldsVo> getFields(@RequestParam Long id){
        if (id == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        TablesFieldsVo tables = tablesService.getFields(id);
        return ResultUtils.success(tables);
    }

    @PostMapping("/test/join")
    public BaseResponse<TableJoinTestVo> testJoin(@RequestBody TableTestJoinDto tableTestJoinDto) {
        if (tableTestJoinDto == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        TableJoinTestVo tableJoinTestVo = tablesService.testJoin(tableTestJoinDto);
        return ResultUtils.success(tableJoinTestVo);
    }
}
